﻿using ManyToManyEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManyToManyEntityFramework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<HomeIndexViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Courses.Select(c => new HomeIndexViewModel()
                {
                    CourseId = c.CourseId,
                    Name = c.Name,
                    NumCredits = c.NumCredits,
                    Students = c.Students_Courses.Select(s => new StudentViewModel()
                    {
                        Grade = s.Student.Grade,
                        Name = s.Student.Name,
                        StudentId = s.Student.StudentId
                    }).ToList()
                }).ToList();
            }
            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}